<?php
require_once("config_tinybrowser.php");
require_once("fns_tinybrowser.php");
require_once('security.php');

// delay script if set
if($tinybrowser['delayprocess']>0) sleep($tinybrowser['delayprocess']);

// Initialise files array and error vars
$files = array();
$good = 0;
$bad = 0;
$dup = 0;
$total = (isset($_GET['filetotal']) ? $_GET['filetotal'] : 0);
$GLOBALS['asdf'] = '';
// Assign get variables
$folder = $tinybrowser['docroot'].urldecode($_GET['folder']);
$passfeid = (isset($_GET['feid']) ? '&feid='.$_GET['feid'] : '');
if ($handle = opendir($folder))
	{
	while (false !== ($file = readdir($handle)))
		{
			$GLOBALS['asdf'] .= $file.',';
		
		if ($file != "." && $file != ".." && substr($file,-1)=='_')
			{
			//-- File Naming
			$tmp_filename = $folder.$file;
			$dest_filename	 = $folder.rtrim($file,'_');
			//-- Duplicate Files
			if(file_exists($dest_filename)) { 
				$fileArr = explode('.', $dest_filename);
				$fileExt = end($fileArr);
				array_pop($fileArr);
				$dest_filename = implode($fileArr);
				$dest_filename .=  '-' . time() . '.' . $fileExt;
				}

			//-- Bad extensions
			$ext = end(explode('.',$dest_filename));
			if(!validateExtension(strtolower($ext), $tinybrowser['prohibited'])) { unlink($tmp_filename); }
        
			//-- Rename temp file to dest file
			rename($tmp_filename, $dest_filename);
			$good++;
			//-- if image, perform additional processing
			if(!validateExtension(strtoupper($ext), $images_arr)) {
				ini_set('memory_limit', '30M');
				//-- Good mime-types
				$imginfo = getimagesize($dest_filename);
				$mime = $imginfo['mime'];
				// resize image to maximum height and width, if set
				if($tinybrowser['imageresize']['width'] > 0 || $tinybrowser['imageresize']['height'] > 0) {
					// assign new width and height values, only if they are less than existing image size
					$widthnew  = ($tinybrowser['imageresize']['width'] > 0 && $tinybrowser['imageresize']['width'] < $imginfo[0] ? $tinybrowser['imageresize']['width'] : $imginfo[0]);
					$heightnew = ($tinybrowser['imageresize']['height'] > 0 && $tinybrowser['imageresize']['height'] < $imginfo[1] ? $tinybrowser['imageresize']['height'] :  $imginfo[1]);
					// only resize if width or height values are different
					if($widthnew != $imginfo[0] || $heightnew != $imginfo[1]) {
						$im = convert_image($dest_filename,$mime);
						resizeimage($im,$widthnew,$heightnew,$dest_filename,$tinybrowser['imagequality'],$mime);
						imagedestroy($im);
					}
				}
				// generate thumbnail
				$thumbimg = $folder."_thumbs/_".rtrim($file,'_');
				if (!file_exists($thumbimg)) {
					$im = convert_image($dest_filename,$mime);
					resizeimage	($im,$tinybrowser['thumbsize'],$tinybrowser['thumbsize'],$thumbimg,$tinybrowser['thumbquality'],$mime);
					imagedestroy ($im);
				}
			}
      	}
		}
	closedir($handle);
	}
$bad = $total-($good+$dup);

// Check for problem during upload
if($total>0 && $bad==$total) Header('Location: ./upload.php?debug='.$GLOBALS['asdf'] .'&type='.$_GET['type'].$passfeid.'&permerror=1&total='.$total.'&badfiles='.$bad.'&goodfiles='.$good.'&dupfiles='.$dup.'&totalfiles='.$total);
else Header('Location: ./upload.php?type='.$_GET['type'].$passfeid.'&badfiles='.$bad.'&goodfiles='.$good.'&dupfiles='.$dup.'&totalfiles='.$total);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>TinyBrowser :: Process Upload</title>
	</head>
	<body>
		<p>You won't see this.</p>
	</body>
</html>
